package com.seari.controller;

import com.seari.bean.*;
import com.seari.dto.EquipmentLedgerAndInfoMaintenanceRequest;
import com.seari.pojo.DataGridBean;
import com.seari.service.EquipmentLedgerAndInfoMaintenanceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备台账和设备信息维护控制类
 *
 * @Author: zyk
 * @Date: 2020/1/3 13:41
 * @Version 1.0
 */
@RestController
@RequestMapping("/equipmentLAIM")
public class EquipmentLedgerAndInfoMaintenanceController {

    @Autowired
    private EquipmentLedgerAndInfoMaintenanceService service;

    /**
     * 资材属性信息列表
     *
     * @param req
     * @return
     */
    @PostMapping("getDeviceList")
    public DataGridBean<ETInformation> getDeviceList(@RequestBody EquipmentLedgerAndInfoMaintenanceRequest req)
    {
        DataGridBean<ETInformation> grid = new DataGridBean<>();
        List<ETInformation> accountList = service.getDeviceAccountBySystemNameAndEtName(req);
        grid.setRows(accountList);
        grid.setTotal(service.getAccountListCount());
        return  grid;
    }

    /**
     * 查看资材属性详情
     *
     * @param eid
     * @return
     */
    @GetMapping("/getTableCodeAndValue/{eid}")
    public Map<String,Object> getTableCodeAndValue(@PathVariable("eid") String eid)
    {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        List<ETInformation> mcAttrList = service.getAttrAndValueByEId(eid);
        if(mcAttrList != null && mcAttrList.size() > 0){
            resultMap.put("code","success");
            resultMap.put("mcAttrList",mcAttrList);
        }else{
            resultMap.put("code","failed");
        }

        List<PZLine> lineList = service.getLineList(null);
        resultMap.put("lineList",lineList);

        List<PZStation> stationList = service.getStationByParam(new Station());
        if(stationList != null && stationList.size() > 0){
            resultMap.put("stationList",stationList);
        }
        return  resultMap;
    }

    /**
     * 通过taskNum获取设备维护记录
     *
     * @param taskNum
     * @return
     */
    @GetMapping("/getETMaintainRecordByTaskNum/{taskNum}")
    public DataGridBean<ETMaintainRecord> getETMaintainRecordByTaskNum(@PathVariable("taskNum") String taskNum)
    {
        DataGridBean<ETMaintainRecord> result = new DataGridBean<>();
        List<ETMaintainRecord> mcAttrList = service.getETMaintainRecordByTaskNum(taskNum);
        result.setRows(mcAttrList);
        return  result;
    }

    /**
     * 保存或更新资材信息
     *
     * @param etInformation
     * @return
     */
    @PostMapping("saveOrUpdateAttrValue")
    public DataGridBean saveOrUpdateAttrValue(@RequestBody ETInformation etInformation) {
        DataGridBean result = new DataGridBean();
        Integer successNum = service.saveOrUpdateAttrValue(etInformation);
        if(successNum < 1){
            result.setMsg("保存或更新资材属性值失败");
        }
        return  result;
    }

    /**
     * 获取导出表格所需的数据
     *
     * @param req
     * @return
     */
    @PostMapping("/downloadCateTemplate")
    public DataGridBean<ETInformationExcel> downloadCateTemplate(@RequestBody EquipmentLedgerAndInfoMaintenanceRequest req)
    {
        DataGridBean<ETInformationExcel> result = new DataGridBean<>();
        // 获得资材分类属性数据
        List<ETInformationExcel> data = service.getFMcAttrByParamsExcel(req);
        result.setRows(data);
        return result;
    }

    /**
     * 删除资材数据
     *
     * @param eid
     * @return
     */
    @GetMapping("deleteMcAttr/{eid}")
    public DataGridBean<ETInformationExcel> deleteMcAttr(@PathVariable("eid")String eid)
    {
        DataGridBean result = new DataGridBean<>();
        int resultNum = service.deleteMcAttrByEId(eid);
        if(resultNum < 0)
        {
            result.setMsg("删除资材属性失败");
        }
        return result;
    }

    /**
     * 获取站点列表
     *
     * @return
     */
    @RequestMapping("/getStationList")
    public DataGridBean<PZStation> getStationList()
    {
        DataGridBean<PZStation> result = new DataGridBean<>();
        List<PZStation> stationList = service.getStationByParam(new Station());
        if(stationList != null && stationList.size() > 0){
            result.setRows(stationList);
            result.setTotal(stationList.size());
        }else{
            result.setMsg("查询站点信息错误");
        }
        return  result;
    }
}
